/**
 *
 *
 *
 *
 *
 */

package org.sxp.modules.sys.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.sxp.common.annotation.SysLog;
import org.sxp.common.utils.PageInfo;
import org.sxp.common.utils.PageResult;
import org.sxp.common.utils.Query;
import org.sxp.common.validator.ValidatorUtils;
import org.sxp.common.web.WrapperResponse;
import org.sxp.modules.sys.entity.SysDictEntity;
import org.sxp.modules.sys.service.SysDictService;

import java.util.Arrays;
import java.util.List;

/**
 * 数据字典
 *
 *
 */
@RestController
@RequestMapping("sys/dict")
@Api(description="Dict", tags = "数据字典表")
@SysLog(modules = "sys", value = "数据字典表")
public class SysDictController extends AbstractController{
    @Autowired
    private SysDictService sysDictService;

    /**
     * 列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum", value = "当前页码", dataType="int", paramType = "header"),
            @ApiImplicitParam(name="pageSize",value="每页条目",dataType="int", paramType = "header"),
            @ApiImplicitParam(name="orderField",value="排序字段",dataType="string", paramType = "header"),
            @ApiImplicitParam(name="orderType",value="排序类型",dataType="string", paramType = "header", example = "asc或者desc")
    })
    @RequestMapping(value = "/list",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value = "查询列表")
    @RequiresPermissions("sys:dict:list")
    public WrapperResponse<List<SysDictEntity>> list(SysDictEntity params) throws Exception{
        //请求头获取分页参数
        PageInfo pageInfo = buildPageInfo();
        IPage<SysDictEntity> page = sysDictService.page(
                new Query<SysDictEntity>().getPage(pageInfo)
                ,new LambdaQueryWrapper<SysDictEntity>()
                .eq(StrUtil.isNotBlank(params.getName()), SysDictEntity::getName, params.getName())
        );

        return (WrapperResponse)WrapperResponse.success(new PageResult(page));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:dict:info")
    public WrapperResponse<SysDictEntity> info(@PathVariable("id") Long id){
        SysDictEntity dict = sysDictService.getById(id);

        return WrapperResponse.success(dict);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:dict:save")
    public WrapperResponse save(@RequestBody SysDictEntity dict){
        //校验类型
        ValidatorUtils.validateEntity(dict);

        sysDictService.save(dict);

        return WrapperResponse.success(null);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:dict:update")
    public WrapperResponse update(@RequestBody SysDictEntity dict){
        //校验类型
        ValidatorUtils.validateEntity(dict);

        sysDictService.updateById(dict);

        return WrapperResponse.success(null);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:dict:delete")
    public WrapperResponse delete(@RequestBody Long[] ids){
        sysDictService.removeByIds(Arrays.asList(ids));

        return WrapperResponse.success(null);
    }

}
